<?php

namespace app\admin\model;

use think\Model;

class GPSCategory extends Model
{   
    protected $table = 'gps_category';
    protected $autoWriteTimestamp = 'datetime';

    const STATUS_NO_UPDATE_TABLE = 1;
    const STATUS_UPDATE_TABLE_SUCCESS = 2;
    const STATUS_DELETE = 3;

    /**
     * 判断是否存在分类信息 
     * Author Raven
     * Date 2018-10-06
     * Params [params]
     * @param  string  $category_name [分类名称]
     */
    public function hasCagegoryName($category_name = '')
    {
        $res = $this
            ->where('category_name', $category_name)
            ->count();

        return $res > 0 ? true : false;
    }

    /**
     * 更新分类状态 
     * Author Raven
     * Date 2018-10-07
     * Params [params]
     * @param  integer $category_id [分类id]
     * @param  integer $status      [分类状态]
     */
    public function updateStatusByCategoryId($category_id = 0, $status = 0)
    {
        $Dao = $this->where('id', $category_id)->find();
        $Dao->status = $status;
        
        return $Dao->save();
    }

    /**
     * 获取活跃的分类列表 
     * Author Raven
     * Date 2018-10-08
     */
    public function getActiveCategoryList()
    {
        $res = $this->where('status', 2)
            ->field(['id', 'category_name'])
            ->order('id', 'desc')
            ->select();

        return $res;
    }
}
